package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.zhouwei.library.CustomPopWindow;
import com.flyco.roundview.RoundTextView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.ExtensionUtilKt;
import com.kmproject.haifu.di.component.DaggerPersonalHomePageActivityComponent;
import com.kmproject.haifu.mvp.contract.PersonalHomePageActivityContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyCommonPublishCountBean;
import com.kmproject.haifu.mvp.model.entity.beans.ScreenData;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserHomePageBean;
import com.kmproject.haifu.mvp.model.entity.beans.ViewStoreInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionAdd;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionCancel;
import com.kmproject.haifu.mvp.presenter.PersonalHomePageActivityPresenter;
import com.kmproject.haifu.mvp.ui.adapter.FragmentTitlesAdapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectPxAdapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectSxAdapter;
import com.kmproject.haifu.mvp.ui.adapter.SelectRegionAdapter;
import com.kmproject.haifu.mvp.ui.fragment.CustomRentingSeekingListFragment;
import com.kmproject.haifu.mvp.ui.fragment.PersonalHpRentingListFragment;
import com.kmproject.haifu.mvp.ui.fragment.PersonalHpStoreFragment;
import com.kmproject.haifu.mvp.ui.fragment.PersonalSenondHandFragment;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.kmproject.haifu.mvp.ui.widget.NoScrollViewPager;
import com.kmproject.haifu.mvp.ui.widget.RoundCornerImageView;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/31/2019 10:34
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class PersonalHomePageActivity extends BaseActivity<PersonalHomePageActivityPresenter> implements PersonalHomePageActivityContract.View {
    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.popuwindows_seelct_sx_RecyclerView)
    RecyclerView popuwindowsSeelctSxRecyclerView;
    @BindView(R.id.popuwindows_seelct_sx_clearTv)
    TextView popuwindowsSeelctSxClearTv;
    @BindView(R.id.popuwindows_seelct_sx_sureTv)
    TextView popuwindowsSeelctSxSureTv;

    @BindView(R.id.rl_member)
    RelativeLayout rl_member;
    @BindView(R.id.personal_hp_imv_user_head_portrait)
    CircleImageView personal_hp_imv_user_head_portrait;
    @BindView(R.id.personal_hp_tv_user_name)
    TextView personal_hp_tv_user_name;
    @BindView(R.id.imv_level_icon)
    ImageView imv_level_icon;
    @BindView(R.id.personal_hp_tv_user_base_info)
    TextView personal_hp_tv_user_base_info;
    @BindView(R.id.personal_hp_rtv_operation)
    RoundTextView personal_hp_rtv_operation;

    @BindView(R.id.rl_store)
    RelativeLayout rl_store;
    @BindView(R.id.imv_store_head_portrait)
    RoundCornerImageView imv_store_head_portrait;
    @BindView(R.id.tv_store_name)
    TextView tv_store_name;
    @BindView(R.id.tv_store_type)
    RoundTextView tv_store_type;
    @BindView(R.id.imv_store_level_icon)
    ImageView imv_store_level_icon;

    @BindView(R.id.personal_hp_ll_2)
    LinearLayout personal_hp_ll_2;
    @BindView(R.id.personal_hp_ll_store_fans)
    LinearLayout personal_hp_ll_store_fans;
    @BindView(R.id.personal_hp_tv_store_fans)
    TextView personal_hp_tv_store_fans;
    @BindView(R.id.personal_hp_tv_store_fans_t)
    TextView personal_hp_tv_store_fans_t;
    @BindView(R.id.personal_hp_ll_store_score)
    LinearLayout personal_hp_ll_store_score;
    @BindView(R.id.personal_hp_tv_store_score)
    TextView personal_hp_tv_store_score;
    @BindView(R.id.personal_hp_tv_store_score_t)
    TextView personal_hp_tv_store_score_t;
    @BindView(R.id.personal_hp_ll_store_page_views)
    LinearLayout personal_hp_ll_store_page_views;
    @BindView(R.id.personal_hp_tv_store_page_views)
    TextView personal_hp_tv_store_page_views;
    @BindView(R.id.personal_hp_tv_store_page_views_t)
    TextView personal_hp_tv_store_page_views_t;

    @BindView(R.id.rtv_personal_hp_chat)
    TextView rtv_personal_hp_chat;
    @BindView(R.id.rtv_personal_hp_call)
    TextView rtv_personal_hp_call;

    @BindView(R.id.ll_head_type_select_layout)
    LinearLayout ll_head_type_select_layout;
    @BindView(R.id.mystore_head_typeselect_RadioGroup)
    RadioGroup mystore_head_typeselect_RadioGroup;
    @BindView(R.id.mystore_head_typeselect_RadioButton1)
    RadioButton mystore_head_typeselect_RadioButton1;
    @BindView(R.id.mystore_head_typeselect_RadioButton2)
    RadioButton mystore_head_typeselect_RadioButton2;

    @BindView(R.id.personal_hp_mi)
    MagicIndicator personalHpMi;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    @BindView(R.id.layout_fragment_header_screen)
    LinearLayout layout_fragment_header_screen;
    @BindView(R.id.tv_filter1)
    TextView tvFilter1;
    @BindView(R.id.tv_filter2)
    TextView tvFilter2;
    @BindView(R.id.tv_filter3)
    TextView tvFilter3;
    @BindView(R.id.tv_filter4)
    TextView tvFilter4;
    @BindView(R.id.imv_filter5)
    ImageView imvFilter5;
    @BindView(R.id.drawer_menu)
    DrawerLayout drawer_menu;
    @BindView(R.id.fragment_container_menu)
    FrameLayout fragment_container_menu;
    private List<String> titles = null;

    private String temPhone;

    /**
     * 0-用户；1-店铺
     */
    private int type = 0;
    private String userId;

    private PersonalHpRentingListFragment personalHpRentingListFragment;
    private CustomRentingSeekingListFragment customRentingSeekingListFragment;
    private PersonalSenondHandFragment  personalSenondHandFragment;
//    private PersonalHpStoreFragment personalHpStoreFragment;

    private UserBean userBean;
    private ScreenData sd;
    private int followFlag;//0-未关注；1-已关注
    List<Fragment> fragments = new ArrayList<>();
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerPersonalHomePageActivityComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_personal_home_page; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        super.onNewIntent(intent);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        sd = new ScreenData();
        userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            assert bundle != null;
            userId = bundle.getString("userId", "");
            //0-用户；1-店铺
            type = bundle.getInt("type", 0);

            if (StringUtils.isTrimEmpty(userId)) {
                userId = AppConstant.PHConstant.phUserId;
            }
            if (type == 0) {
                type = AppConstant.PHConstant.phType;
            }
        }

        if (userId != null && userBean != null && userId.equals(userBean.getUserId())) {
            setTitle(getString(R.string.personal_hp_title_1));
        } else {
            if (type == 1) {
                setTitle(getString(R.string.personal_hp_title_3));
            } else {
                setTitle(getString(R.string.personal_hp_title_2));
            }
        }

        drawer_menu.closeDrawer(Gravity.END);

        personalHpRentingListFragment = PersonalHpRentingListFragment.newInstance(userId, type);
        fragments.add(personalHpRentingListFragment);
        personalSenondHandFragment=new PersonalSenondHandFragment().newInstance(userId, type);
        fragments.add(personalSenondHandFragment);
        if (type == 0) {
            rl_member.setVisibility(View.VISIBLE);
            rl_store.setVisibility(View.GONE);
            customRentingSeekingListFragment = CustomRentingSeekingListFragment.newInstance(userId, 0);
            fragments.add(customRentingSeekingListFragment);
//            fragments.add("售房");
//            fragments.add("二手物品");
            titles = Arrays.asList(getResources().getStringArray(R.array.personal_page_tab_title_1));
        } else {
            rl_member.setVisibility(View.GONE);
            rl_store.setVisibility(View.VISIBLE);
//            personalHpStoreFragment = PersonalHpStoreFragment.newInstance();//.setData("1156583687672352770")
//            fragments.add(personalHpStoreFragment);
            titles = Arrays.asList(getResources().getStringArray(R.array.personal_page_tab_title_2));
        }
        getBaseInfo();

    }

    /**
     * 初始化标签显示
     *
     * @param bean
     */
    public void initMenu(UserHomePageBean bean){
        titles.set(0,titles.get(0)+"("+bean.getUserCount().getHouseRentCount()+")");
        titles.set(1,titles.get(1)+"("+bean.getUserCount().getSecondHandCount()+")");
        titles.set(2,titles.get(2)+"("+bean.getUserCount().getHouseRentSeekingCount()+")");
        viewpager.setAdapter(new FragmentTitlesAdapter(getSupportFragmentManager(), this, titles, fragments));
        viewpager.setCurrentItem(0);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
//                simplePagerTitleView.setWidth((ScreenUtils.getScreenWidth() - 30) / 3);
                simplePagerTitleView.setWidth(ScreenUtils.getScreenWidth() / titles.size());
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER;
//                layoutParams.setMargins(0, 0, 0, 0);//4个参数按顺序分别是左上右下
                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(v -> {
                    personalHpMi.onPageSelected(index);
                    commonNavigator.getAdapter().notifyDataSetChanged();
                    viewpager.setCurrentItem(index);
                    if (index == 2) {
//                        if (personalHpStoreFragment != null) {
//                            personalHpStoreFragment.setData(userId);// todo test 1156583687672352770
//                        }
                    }

                    switch (index) {
                        case 0:
                            if (type == 0) {
                                layout_fragment_header_screen.setVisibility(View.GONE);
                            } else {
                                layout_fragment_header_screen.setVisibility(View.VISIBLE);
                            }
                            ll_head_type_select_layout.setVisibility(View.VISIBLE);
                            sd.setCategoryId(mCategoryId);
                            personalHpRentingListFragment.setData(sd);
                            break;
                        case 1:
                            //二手物品
                            layout_fragment_header_screen.setVisibility(View.GONE);
                            ll_head_type_select_layout.setVisibility(View.GONE);

                            break;
                        case 2:
                            //求租
                            if (customRentingSeekingListFragment != null) {
                                sd.setCategoryId(mCategoryId);
                                customRentingSeekingListFragment.setData(sd);
                            }
                        case 3:
                            layout_fragment_header_screen.setVisibility(View.GONE);
                            ll_head_type_select_layout.setVisibility(View.GONE);
                            break;
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(11.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(PersonalHomePageActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });

        personalHpMi.setNavigator(commonNavigator);
        ViewPagerHelper.bind(personalHpMi, viewpager);
        assert mPresenter != null;
        // todo 测试
//        userId = "521677655146233856";
        mPresenter.dictList4Category(Api.HOUSE_CATEGORY);

        if (type == 0) {
            layout_fragment_header_screen.setVisibility(View.GONE);
        } else {
            // todo 需要去查询筛选列表，或者点击试图时再去获取
            layout_fragment_header_screen.setVisibility(View.VISIBLE);
        }

        mystore_head_typeselect_RadioGroup.setOnCheckedChangeListener((group, checkedId) -> chooseCategory());
    }

    private void getBaseInfo() {
        assert mPresenter != null;
        if (type == 1) {
            mPresenter.viewStoreInfo(userId);
        } else {
            mPresenter.getUserHomePageInfo(userId);
        }
        mPresenter.myCommonPublishCount();
    }

    @OnClick({R.id.tv_filter1, R.id.tv_filter2, R.id.tv_filter3, R.id.tv_filter4, R.id.imv_filter5, R.id.personal_hp_rtv_operation,
            R.id.rtv_personal_hp_chat, R.id.rtv_personal_hp_call, R.id.popuwindows_seelct_sx_clearTv, R.id.popuwindows_seelct_sx_sureTv,})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_filter1:
                showLocationView();
                break;
            case R.id.tv_filter2:
                showRentView();
                break;
            case R.id.tv_filter3:
                break;
            case R.id.tv_filter4:
                showScreenView();
                break;
            case R.id.imv_filter5:
                showSortView();
            case R.id.personal_hp_rtv_operation:
                if (followFlag == 1) {
                    ReqParamsAttentionCancel reqParamsAttentionCancel = new ReqParamsAttentionCancel();
                    reqParamsAttentionCancel.setIds(Collections.singletonList(userId));
                    reqParamsAttentionCancel.setType(type);
                    assert mPresenter != null;
                    mPresenter.unFollow(reqParamsAttentionCancel);
                } else {
                    ReqParamsAttentionAdd reqParamsAttentionAdd = new ReqParamsAttentionAdd();
                    reqParamsAttentionAdd.setId(userId);
                    reqParamsAttentionAdd.setType(type);
                    assert mPresenter != null;
                    mPresenter.follow(reqParamsAttentionAdd);
                }
                break;
            case R.id.popuwindows_seelct_sx_sureTv:
                setDrawerMenu();
                //todo 设置参数，并发起网络请求
                ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
                break;
            case R.id.popuwindows_seelct_sx_clearTv:
                ArmsUtils.makeText(this, "清除选择");
                break;
            case R.id.rtv_personal_hp_chat:
                PhoneUtils.dial(temPhone);// todo 暂时拨打电话
                break;
            case R.id.rtv_personal_hp_call:
                PhoneUtils.dial(temPhone);
                break;
        }
    }

    /**
     * 打开或关闭侧滑菜单
     */
    public void setDrawerMenu() {
        if (drawer_menu.isDrawerOpen(Gravity.END)) {
            drawer_menu.closeDrawer(Gravity.END);
        } else {
            drawer_menu.openDrawer(Gravity.END);
        }
    }

    private CustomPopWindow mCustomPopWindow4Rent;
    private View rentView;
    private TextView etMinPrice;
    private TextView etMaxPrice;
    private TextView etSure;

    private void showRentView() {
        if (rentView == null) {
            rentView = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_zj, null);
            etMinPrice = rentView.findViewById(R.id.popuwindows_seelct_zj_zdj);
            etMaxPrice = rentView.findViewById(R.id.popuwindows_seelct_zj_zgj);
            etSure = rentView.findViewById(R.id.popuwindows_seelct_zj_btn);
            etSure.setOnClickListener(v -> {
                if (StringUtils.isEmpty(etMinPrice.getText())) {
                    ArmsUtils.makeText(PersonalHomePageActivity.this, "请填写最低价");
                    return;
                }
                if (StringUtils.isEmpty(etMaxPrice.getText())) {
                    ArmsUtils.makeText(PersonalHomePageActivity.this, "请填写最高价");
                    return;
                }
                double minPrice = Double.parseDouble(etMinPrice.getText().toString());
                double maxPrice = Double.parseDouble(etMaxPrice.getText().toString());
                if (minPrice > maxPrice) {
                    ArmsUtils.makeText(PersonalHomePageActivity.this, "最高价必须大于最低价");
                    return;
                }
                mCustomPopWindow4Rent.dissmiss();
                //todo 设置参数，并发起网络请求
                ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
            });
        }
        mCustomPopWindow4Rent = initDropDownMenu(rentView, tvFilter2, ConvertUtils.dp2px(360), R.drawable.houserentingshow_selection_noselect_down, R.drawable.houserentingshow_selection_selectdown);
    }

    private CustomPopWindow mCustomPopWindow4Sort;
    private View sortView;
    private RecyclerView rvSort;
    private PopuWindowSelectPxAdapter popuWindowSelectPxAdapter;

    private void showSortView() {
        if (sortView == null) {
            sortView = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_px, null);
            rvSort = sortView.findViewById(R.id.popuwindows_seelct_px_RecyclerView);
            rvSort.setLayoutManager(new LinearLayoutManager(PersonalHomePageActivity.this));
            popuWindowSelectPxAdapter = new PopuWindowSelectPxAdapter(Arrays.asList(getResources().getStringArray(R.array.searchpx)), (index, content) -> {
                ExtensionUtilKt.dataToSearch((String) content);
                mCustomPopWindow4Sort.dissmiss();
                //todo 设置参数，并发起网络请求
                ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
            });
            rvSort.setAdapter(popuWindowSelectPxAdapter);
            popuWindowSelectPxAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        }
        mCustomPopWindow4Sort = initDropDownMenu(sortView, imvFilter5, ViewGroup.LayoutParams.WRAP_CONTENT, R.drawable.houserentingshow_selection_noselect_tj, R.drawable.houserentingshow_selection_select_tj);
    }

    private CustomPopWindow mCustomPopWindow4Location;
    private View locationView;
    private RecyclerView popupWindowsSelectAddressRv_1;
    private RecyclerView popupWindowsSelectAddressRv_2;
    private RecyclerView popupWindowsSelectAddressRv_3;
    private View v_split_line_1;
    private View v_split_line_2;
    private SelectRegionAdapter mSelectRegionAdapter1;
    private SelectRegionAdapter mSelectRegionAdapter2;
    private SelectRegionAdapter mSelectRegionAdapter3;

    private void showLocationView() {
        if (AppConstant.houseArea.size() < 1) {
//            HousePayWayBean bean;
//            HousePayWayBean bean2;
//            HousePayWayBean bean3;
//            List<HousePayWayBean> beanList2;
//            List<HousePayWayBean> beanList3;
//            for (int i = 0; i < 20; i++) {
//                bean = new HousePayWayBean();
//                bean.setName("name-" + i);
//                beanList2 = new ArrayList<>();
//                for (int j = 0; j < 15; j++) {
//                    bean2 = new HousePayWayBean();
//                    bean2.setName("name-" + i + "-" + j);
//                    beanList3 = new ArrayList<>();
//                    for (int k = 0; k < 10; k++) {
//                        bean3 = new HousePayWayBean();
//                        bean3.setName("name-" + i + "-" + j + "-" + k);
//                        beanList3.add(bean3);
//                    }
//                    bean2.setChildren(beanList3);
//                    beanList2.add(bean2);
//                }
//                bean.setChildren(beanList2);
//                AppConstant.houseArea.add(bean);
//            }
            assert mPresenter != null;
            mPresenter.getAreaList(Api.HOUSE_AREA);
        } else {

            if (locationView == null) {
                locationView = LayoutInflater.from(this).inflate(R.layout.popuwindows_select_address, null);
                popupWindowsSelectAddressRv_1 = locationView.findViewById(R.id.popup_windows_select_address_rv_1);
                popupWindowsSelectAddressRv_2 = locationView.findViewById(R.id.popup_windows_select_address_rv_2);
                popupWindowsSelectAddressRv_3 = locationView.findViewById(R.id.popup_windows_select_address_rv_3);
                v_split_line_1 = locationView.findViewById(R.id.v_split_line_1);
                v_split_line_2 = locationView.findViewById(R.id.v_split_line_2);

                LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                ArmsUtils.configRecyclerView(popupWindowsSelectAddressRv_1, layoutManager);
                ArmsUtils.configRecyclerView(popupWindowsSelectAddressRv_2, new LinearLayoutManager(this));
                ArmsUtils.configRecyclerView(popupWindowsSelectAddressRv_3, new LinearLayoutManager(this));

                mSelectRegionAdapter1 = new SelectRegionAdapter(AppConstant.houseArea);
                mSelectRegionAdapter2 = new SelectRegionAdapter(new ArrayList<>());
                mSelectRegionAdapter3 = new SelectRegionAdapter(new ArrayList<>());

                popupWindowsSelectAddressRv_1.setAdapter(mSelectRegionAdapter1);
                popupWindowsSelectAddressRv_2.setAdapter(mSelectRegionAdapter2);
                popupWindowsSelectAddressRv_3.setAdapter(mSelectRegionAdapter3);
                mSelectRegionAdapter1.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
                mSelectRegionAdapter2.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
                mSelectRegionAdapter3.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);

                mSelectRegionAdapter1.setOnItemClickListener((adapter, view, position) -> {
                    if (mSelectRegionAdapter1.getData().get(position).getChildren() != null && mSelectRegionAdapter1.getData().get(position).getChildren().size() > 0) {
                        v_split_line_1.setVisibility(View.VISIBLE);
                        popupWindowsSelectAddressRv_2.setVisibility(View.VISIBLE);
                        mSelectRegionAdapter2.setNewData(mSelectRegionAdapter1.getData().get(position).getChildren());
                        popupWindowsSelectAddressRv_2.smoothScrollToPosition(0);
                    } else {
                        HousePayWayBean bean = mSelectRegionAdapter1.getData().get(position);
                        tvFilter1.setText(bean.getName());
                        mCustomPopWindow4Location.dissmiss();
                        //todo 设置参数，并发起网络请求
                        ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
                    }
                });
                mSelectRegionAdapter2.setOnItemClickListener((adapter, view, position) -> {
                    if (mSelectRegionAdapter2.getData().get(position).getChildren() != null && mSelectRegionAdapter2.getData().get(position).getChildren().size() > 0) {
                        v_split_line_2.setVisibility(View.VISIBLE);
                        popupWindowsSelectAddressRv_3.setVisibility(View.VISIBLE);
                        mSelectRegionAdapter3.setNewData(mSelectRegionAdapter2.getData().get(position).getChildren());
                        popupWindowsSelectAddressRv_3.smoothScrollToPosition(0);
                    } else {
                        HousePayWayBean bean = mSelectRegionAdapter2.getData().get(position);
                        tvFilter1.setText(bean.getName());
                        mCustomPopWindow4Location.dissmiss();
                        //todo 设置参数，并发起网络请求
                        ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
                    }
                });
                mSelectRegionAdapter3.setOnItemClickListener((adapter, view, position) -> {
                    HousePayWayBean bean = mSelectRegionAdapter3.getData().get(position);
                    tvFilter1.setText(bean.getName());
                    mCustomPopWindow4Location.dissmiss();
                    //todo 设置参数，并发起网络请求
                    ArmsUtils.makeText(PersonalHomePageActivity.this, "设置参数，并发起网络请求");
                });
            }

            int height;
            if (AppConstant.houseArea.size() > 12) {
                height = ConvertUtils.dp2px(360);
            } else {
                height = ConvertUtils.dp2px(ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            mCustomPopWindow4Location = initDropDownMenu(locationView, tvFilter1, height, R.drawable.houserentingshow_selection_noselect_down, R.drawable.houserentingshow_selection_selectdown);
        }
    }

    private CustomPopWindow initDropDownMenu(View contentView, View filterView, int height, @DrawableRes int nId, @DrawableRes int sId) {
        ViewGroup parentViewGroup = (ViewGroup) contentView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViews();
        }
        //创建并显示popWindow
        CustomPopWindow mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, height)
                .setAnimationStyle(R.style.popupWindowAnimationScale)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create();
        mCustomPopWindow.getPopupWindow().setOnDismissListener(() -> {
            if (filterView instanceof TextView) {
//                ((TextView) filterView).setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(this, R.drawable.houserentingshow_selection_noselect_down), null);
                ((TextView) filterView).setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(this, nId), null);
            } else if (filterView instanceof ImageView) {
                ((ImageView) filterView).setImageResource(nId);
            }
//                vHalfWhiteLayout.setVisibility(View.GONE);
            mCustomPopWindow.dissmiss();
        });
        if (filterView instanceof TextView) {
//            ((TextView) filterView).setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(this,  R.drawable.houserentingshow_selection_selectdown), null);
            ((TextView) filterView).setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(this, sId), null);
        } else if (filterView instanceof ImageView) {
            ((ImageView) filterView).setImageResource(sId);
        }
        mCustomPopWindow.showAsDropDown(layout_fragment_header_screen, 0, 0);

//        vHalfWhiteLayout.setVisibility(View.VISIBLE);
        return mCustomPopWindow;
    }

    private PopuWindowSelectSxAdapter popuWindowSelectSxAdapter;

    private void showScreenView() {
        if (screenList != null && screenList.size() > 0) {
            if (popuWindowSelectSxAdapter == null) {
                popuwindowsSeelctSxRecyclerView.setLayoutManager(new LinearLayoutManager(PersonalHomePageActivity.this));
                popuWindowSelectSxAdapter = new PopuWindowSelectSxAdapter(screenList);
                popuwindowsSeelctSxRecyclerView.setAdapter(popuWindowSelectSxAdapter);
                popuWindowSelectSxAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);

//                popuWindowSelectSxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//
//                    }
//                });
            }
            setDrawerMenu();
        } else {
            assert mPresenter != null;
            mPresenter.dictList4Screen(Api.HOUSE_AREA);
        }
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (drawer_menu.isDrawerOpen(Gravity.END)) {
                drawer_menu.closeDrawer(Gravity.END);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showUserInfo(UserHomePageBean bean) {
        if (bean != null) {
            initMenu(bean);
            if (bean.getBaseUser() != null) {
                String sex = bean.getBaseUser().getUserSex();
                if (StringUtils.isEmpty(sex)) {
//                    personal_hp_tv_user_base_info.setText(String.format(getString(R.string.rent_seeking_detail_fm_6), bean.getCreateTimeCount(), "", bean.getLoginTimeCount()));
                    personal_hp_tv_user_base_info.setText(bean.getCreateTimeCount());
                } else {
//                    personal_hp_tv_user_base_info.setText(String.format(getString(R.string.rent_seeking_detail_fm_6), bean.getCreateTimeCount(), sex + "，", bean.getLoginTimeCount()));
                    personal_hp_tv_user_base_info.setText(bean.getCreateTimeCount());
                }
                getMglideImage().loadImage(this, ImageConfigImpl.builder().url(bean.getBaseUser().getAvatar())
                        .imageView(personal_hp_imv_user_head_portrait)
                        .build());
//                if (!StringUtils.isEmpty(bean.getBaseUser().getNickName())) {
//                    personal_hp_tv_user_name.setText(bean.getBaseUser().getNickName());
//                } else {
//                    personal_hp_tv_user_name.setText(bean.getBaseUser().getUserName());
//                }
                personal_hp_tv_user_name.setText(bean.getBaseUser().getNickName());

                personal_hp_ll_store_page_views.setVisibility(View.GONE);
                personal_hp_tv_store_fans_t.setText(getString(R.string.follow));
                personal_hp_tv_store_score_t.setText(getString(R.string.personal_hp_fans));
                personal_hp_tv_store_fans.setText(String.valueOf(bean.getAttentionCount()));
                personal_hp_tv_store_score.setText(String.valueOf(bean.getFansCount()));
                personal_hp_ll_store_fans.setOnClickListener(v -> {
                    //跳关注列表
                    Intent intent = new Intent(PersonalHomePageActivity.this, FollowListActivity.class);
                    assert userBean != null;
                    intent.putExtra("userId", userBean.getUserId());
                    ArmsUtils.startActivity(intent);
                });
                personal_hp_ll_store_score.setOnClickListener(v -> {
                    //跳粉丝列表
                    Intent intent = new Intent(PersonalHomePageActivity.this, FansListActivity.class);
                    intent.putExtra("userId", userId);
                    intent.putExtra("type", 0);
                    ArmsUtils.startActivity(intent);
                });
                if (userBean != null && userBean.getUserId().equals(bean.getBaseUser().getUserId())) {
                    personal_hp_rtv_operation.setText(getString(R.string.edit));
                    personal_hp_rtv_operation.setOnClickListener(v -> ArmsUtils.startActivity(new Intent(PersonalHomePageActivity.this, UserSetEditInfoActivity.class)));
                } else {
                    followFlag = bean.getIsAttention();
                    if (followFlag == 1) {// 0-未关注；1-已关注
                        personal_hp_rtv_operation.setText(getString(R.string.personal_hp_follow_cancel));
                        personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_AAAAAA));
                        personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.white));
                    } else {
                        personal_hp_rtv_operation.setText(getString(R.string.follow_add));
                        personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_F9D35A));
                        personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.common_title_text));
                    }
                }
            }
            UserHomePageBean.MemberLevelBean memberLevelBean = bean.getMemberLevel();
            if (memberLevelBean != null) {
                getMglideImage().loadImage(this, ImageConfigImpl.builder().url(memberLevelBean.getHeadImage())
                        .imageView(imv_level_icon)
                        .build());
            }

            if ("86".equals(bean.getBaseUser().getMobile().substring(0, 2))) {
                temPhone = bean.getBaseUser().getMobile().substring(2);
            } else {
                temPhone = bean.getBaseUser().getMobile();
            }
        }
    }

    @Override
    public void myCommonPublishCountSuccess(MyCommonPublishCountBean bean) {
        // todo 设置tab切换
    }

    @Override
    public void viewStoreInfo(ViewStoreInfoBean bean) {
        if (bean != null) {
            getMglideImage().loadImage(this, ImageConfigImpl.builder().url(bean.getStoreLogo()).imageRadius(5)
                    .imageView(imv_store_head_portrait.setmBorderRadius(20))
                    .build());
            tv_store_name.setText(bean.getStoreName());
            tv_store_type.setText(bean.getStoreTypeName());

            getMglideImage().loadImage(this, ImageConfigImpl.builder().url(bean.getUserInfo().getLevelHeadImage())
                    .imageView(imv_store_level_icon)
                    .build());

            personal_hp_tv_store_fans.setText(String.valueOf(bean.getAttentionCount()));

            personal_hp_ll_store_fans.setOnClickListener(v -> {
                //跳粉丝列表
                Intent intent = new Intent(PersonalHomePageActivity.this, FansListActivity.class);
                intent.putExtra("userId", userId);
                intent.putExtra("type", 1);
                ArmsUtils.startActivity(intent);
            });
            personal_hp_tv_store_score.setText(String.valueOf(bean.getScore()));
            personal_hp_ll_store_page_views.setVisibility(View.VISIBLE);
            personal_hp_tv_store_page_views.setText(String.valueOf(bean.getViewCount()));
            followFlag = bean.getIsAttention();
            if (followFlag == 1) {// 0-未关注；1-已关注
                personal_hp_rtv_operation.setText(getString(R.string.personal_hp_follow_cancel));
                personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_AAAAAA));
                personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.white));
            } else {
                personal_hp_rtv_operation.setText(getString(R.string.follow_add));
                personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_F9D35A));
                personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.common_title_text));
            }
        }
    }

    @Override
    public void followSuccess() {
        followFlag = 1;
        personal_hp_rtv_operation.setText(getString(R.string.personal_hp_follow_cancel));
        personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_AAAAAA));
        personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.white));
        showMessage(getString(R.string.success));
        getBaseInfo();
    }

    @Override
    public void unFollowSuccess() {
        followFlag = 0;
        personal_hp_rtv_operation.setText(getString(R.string.follow_add));
        personal_hp_rtv_operation.getDelegate().setBackgroundColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.color_F9D35A));
        personal_hp_rtv_operation.setTextColor(ContextCompat.getColor(PersonalHomePageActivity.this, R.color.common_title_text));
        showMessage(getString(R.string.success));
        getBaseInfo();
    }

    private List<HousePayWayBean> categoryList = new ArrayList<>();
    private List<HouseInfoTitleBean> screenList = new ArrayList<>();
    private String mCategoryId;// todo 设置条件筛选项

    @Override
    public void getCategorySuccess(List<HousePayWayBean> beanList) {
        if (!beanList.isEmpty()) {
            categoryList.clear();
            categoryList.addAll(beanList);
            chooseCategory();
            personalHpRentingListFragment.setData(sd);
        }
    }

    private void chooseCategory() {
        if (mystore_head_typeselect_RadioButton1.isChecked()) {
            mCategoryId = categoryList.get(0).getDictId();
        } else {
            if (categoryList.size() > 1) {
                mCategoryId = categoryList.get(1).getDictId();
            }
        }
        sd.setCategoryId(mCategoryId);
    }

    @Override
    public void getAreaListSuccess(List<HousePayWayBean> beanList) {
        if (!beanList.isEmpty()) {
            AppConstant.houseArea.clear();
            AppConstant.houseArea.addAll(beanList);
            showLocationView();
        }
    }

    @Override
    public void getScreenSuccess(List<HouseInfoTitleBean> beanList) {
        if (!beanList.isEmpty()) {
            screenList.clear();
            screenList.addAll(beanList);
            showScreenView();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppConstant.PHConstant.phUserId = null;
        AppConstant.PHConstant.phType = 0;
    }
}
